package com.aboo.es.action;

import com.aboo.es.service.MovieService;
import com.aboo.es.vo.Movie;
import com.aboo.frame.vo.ReturnVO;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/es")
public class MovieAction {

    @Resource
    private MovieService movieService;
    @Resource
    private ElasticsearchTemplate elasticsearchTemplate;

    @RequestMapping("/movie/saveMovie")
    public ReturnVO<Object> saveMovie(String id, String name, String desc) {
        ReturnVO<Object> rv = new ReturnVO();
        movieService.saveMovie(id, name, desc);

        return rv;
    }

    @RequestMapping("/movie/getMovieById")
    public ReturnVO<Object> getMovieById(String id) {
        ReturnVO<Object> rv = new ReturnVO<>();
        Movie movie = movieService.getById(id);
        rv.setData(movie);

        return rv;
    }

    @RequestMapping("/movie/getAllMovie")
    public ReturnVO<List<Movie>> getAllMovie() {
        ReturnVO<List<Movie>> rv = new ReturnVO<>();
        List<Movie> movies = movieService.getAll();
        rv.setData(movies);

        return rv;
    }

    @RequestMapping("/movie/updateMovie")
    public ReturnVO<Object> updateMovie(String id, String name, String desc) {
        ReturnVO<Object> rv = new ReturnVO();
        movieService.updateMovie(id, name, desc);

        return rv;
    }

    @RequestMapping("/movie/deleteById")
    public ReturnVO<Object> deleteById(String id) {
        ReturnVO<Object> rv = new ReturnVO();
        movieService.deleteById(id);

        return rv;
    }

    @RequestMapping("/movie/getByName")
    public ReturnVO<List<Movie>> getByName(String name) {
        ReturnVO<List<Movie>> rv = new ReturnVO<>();
        List<Movie> movies = movieService.getByName(name);
        rv.setData(movies);

        return rv;
    }

    @RequestMapping("/movie/getPageByKw")
    public ReturnVO<List<Movie>> getPageByKw(String keyword, int page, int size) {
        ReturnVO<List<Movie>> rv = new ReturnVO<>();

        List<Movie> data = new ArrayList<>(16);
        Page<Movie> pageable = movieService.findByKey(keyword, page, size);
        Iterator<Movie> iterator = pageable.iterator();
        while (iterator.hasNext()) {
            data.add(iterator.next());
        }

        rv.setData(data);
        return rv;
    }

    @RequestMapping("/movie/searchKw")
    public ReturnVO<List<Movie>> searchKw(String keyword) {
        ReturnVO<List<Movie>> rv = new ReturnVO<>();

        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.queryStringQuery(keyword))
                .build();
        List<Movie> list = elasticsearchTemplate.queryForList(searchQuery, Movie.class);
        rv.setData(list);

        return rv;
    }

}
